package com.local.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.threads.DeleteSetRuleRunble;
import com.cloud.threads.SetRuleRunble;
import com.cz073586.tools.util.ThreadPoolUtil;
import com.cz073586.tools.util.Tools;
import com.local.manager.cfgManager.CloudConfigManager;
import com.local.model.LocalCarType;
import com.local.model.LocalChargeBytime;
import com.local.model.LocalWeekSection;
import com.local.page.LocalChargeByTimePage;
import com.local.service.face.LocalCarTypeService;
import com.local.service.face.LocalChargeByTimeService;
import com.local.service.face.LocalWeekSectionService;
import com.local.service.face.LogService;
import com.qianhai.common.constants.CommonContans;
import com.qianhai.common.controller.BaseController;

@RestController
@RequestMapping(value = "/admin/charge", method = RequestMethod.POST)
public class LocalChargeByTimeController extends BaseController
{
	
	@Autowired
	private LocalChargeByTimeService localChargeByTimeService;
	
	@Autowired
	private LocalWeekSectionService localWeekSectionService;
	
	@Autowired
	private LocalCarTypeService localCarTypeService;
	
	@Autowired
	private LogService logService;
	
	@RequestMapping(value = "/queryChargeByPage.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryChargeByPage(
			@RequestBody LocalChargeByTimePage<LocalChargeBytime> page)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			localChargeByTimeService.queryChargeByPage(page);
			resultMap.put(CommonContans.STRUTS_DATA, page);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
			
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	
	
	@RequestMapping(value = "/addOrModifyLocalChargeBytime.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap addOrModifyLocalChargeBytime(
			@RequestBody LocalChargeBytime localChargeBytime)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			localChargeByTimeService.saveOrUpdateParkwellMember(localChargeBytime);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: querychargeBytimeById
	 * @描述: 通过临时车收费类型ID查询
	 *
	 * @参数:
	 * @param chargeBytimeId
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:zhangw
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryChargeBytimeById.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap querychargeBytimeById(@RequestBody LocalChargeBytime localChargeBytime)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			localChargeBytime=localChargeByTimeService.selectByPrimaryKey(localChargeBytime.getChargeBytimeUuid());
			LocalWeekSection lws=localWeekSectionService.loadAllWeekSectionByChargUuid(localChargeBytime.getChargeBytimeUuid());
			if(!Tools.isEmpty(localChargeBytime)){
				if(!Tools.isEmpty(localChargeBytime.getCarType())){
					LocalCarType lc=localCarTypeService.selectByPrimaryKey(localChargeBytime.getCarType());
					localChargeBytime.setCartypeName(!Tools.isEmpty(lc)&&!Tools.isEmpty(lc.getCartypeName())?lc.getCartypeName():"小型车");
				}
				if(!Tools.isEmpty(lws)){
					localChargeBytime.setWeekUuid(lws.getUuid());
					localChargeBytime.setType(Tools.isEmpty(lws.getType())?"3":lws.getType());
					localChargeBytime.setSection(Tools.isEmpty(lws.getSection())?"2":lws.getSection());
					if(!Tools.isEmpty(lws.getSectionStartTime())){
						localChargeBytime.setSectionStartTime(lws.getSectionStartTime());
					}
					if(!Tools.isEmpty(lws.getSectionEndTime())){
						localChargeBytime.setSectionEndTime(lws.getSectionEndTime());
					}
				}
			}
			resultMap.put(CommonContans.STRUTS_DATA,localChargeBytime
					);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：根据主键删除临时车收费类型
	 * 
	 * @Title: deletechargeBytime
	 * @param @param chargeBytimeId
	 * @param @return
	 * @return ModelMap
	 * @chargeBytime zhangw 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/deleteChargeBytime.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap deletechargeBytime(@RequestBody List<String> ids)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			localChargeByTimeService.deletechargeBytime(ids);
			
				//计费规则，上传到云平台
				if (CloudConfigManager.instance().isOpen())
				{
					DeleteSetRuleRunble upload = new DeleteSetRuleRunble(
							ids, logService);
					ThreadPoolUtil.instance().execute(upload);
				}
			
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：根据主键修改计费规则是否生效
	 * 
	 * @Title: deletechargeBytime
	 * @param @param chargeBytimeId
	 * @param @return
	 * @return ModelMap
	 * @chargeBytime zhangw 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/updateChargeBytime.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap updateChargeBytime(@RequestBody List<String> ids,Integer status)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			List<LocalChargeBytime> lis=localChargeByTimeService.updateChargeBytime(ids,status);
			if(!Tools.isEmpty(lis)&&lis.size()>0){
				for (LocalChargeBytime ls : lis)
				{
					//计费规则，上传到云平台
					if (CloudConfigManager.instance().isOpen())
					{
						SetRuleRunble upload = new SetRuleRunble(
								ls, logService);
						ThreadPoolUtil.instance().execute(upload);
					}
				}
			}
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
}
